import { ColorEnum } from '@/enum/graph.enum'
import type { ColorInfo, GraphSize } from '@/types/global'

export function getToken() {
  return sessionStorage.getItem('token')
}

export function getGraphColorList(str:string) {
  const list:ColorInfo[] = []
  if (!str) {
    return list
  }
  str.split('_').forEach((item) => {
    const color = ColorEnum[item as keyof typeof ColorEnum]
    if (color) {
      list.push(color)
    }
  })
  return list
}
export function getSize(str:string):GraphSize {
  const size:GraphSize = {
    width: 0,
    height: 0
  }
  if (!str) {
    console.log('getSize-error:获取画布大小失败')
    return size
  }

  // 使用 split 方法将字符串按 * 分割成数组
  const parts = str.split('×')
  if (parts.length !== 2) {
    console.log('getSize-error:字符串格式错误')
    return size
  }

  const width = parseInt(parts[0].trim(), 10)
  const height = parseInt(parts[1].trim(), 10)

  if (isNaN(width) || isNaN(height)) {
    console.log('getSize-error:解析数字失败')
    return size
  }

  return {
    width,
    height
  }
}
export function serializeRequestForm(object:object) {
  return Object.entries(object).map(([key, value]) => ({
    K: key,
    V: value
  }))
}
